﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.IO;
using Tesla.Content;
using D3DC = SlimDX.D3DCompiler;

namespace Tesla.Direct3D10.Content.Loaders {
    /// <summary>
    /// D3D Compiler effect header include helper.
    /// </summary>
    internal sealed class EffectInclude : D3DC.Include {
        private ContentManager _manager;
        private String _path;

        /// <summary>
        /// Creates a new EffectInclude with specified content manager and directory of the effect file.
        /// </summary>
        /// <param name="manager">Content Manager</param>
        /// <param name="path">Effect file directory</param>
        public EffectInclude(ContentManager manager, String path) {
            _manager = manager;
            _path = path;
        }

        /// <summary>
        /// Closes the underlying input stream.
        /// </summary>
        /// <param name="stream"></param>
        public void Close(Stream stream) {
            stream.Close();
        }

        /// <summary>
        /// Opens an input stream to read the include file.
        /// </summary>
        /// <param name="type">Type of include (ignored)</param>
        /// <param name="fileName">Name of the include file</param>
        /// <param name="parentStream">Paren's stream</param>
        /// <param name="stream">Stream to return</param>
        public void Open(D3DC.IncludeType type, string fileName, Stream parentStream, out Stream stream) {
            if(String.IsNullOrEmpty(fileName)) {
                throw new ArgumentNullException("fileName", "File name cannot be null");
            }

            stream = _manager.OpenStream(Path.Combine(_path, fileName));
        }
    }
}
